package ro.jproject.imc.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import ro.jproject.imc.model.DataBaseConnection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.JTextField;


public class UpdateStudentsDataBase extends JFrame {

	private static final long serialVersionUID = 1L;
	private JFrame frame;
	public String id;
	public String firstName;
	public String lastName;
	public String cnp;
	public String status;
	public String group;
	public String facultyName;
	public String specialization;
	public String yearOfStudy;
	public String registrationYear;
	public String idCol;
	public static DefaultTableModel model;
	public static JTable table;
	private JTextField textFieldCnp;
	private JTextField textFieldLastName;
	private JTextField textFieldFirstName;
	private JTextField textFieldStatus;
	private JTextField textFieldGroup;
	private JTextField textFieldFaculty;
	
	public UpdateStudentsDataBase(){

		

		setTitle("Update Students DataBase");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(150, 100, 299, 273);
		setSize(1107, 494);
		
		frame = new JFrame();
		Container container = getContentPane();
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		scrollPane.setBounds(10, 11, 1071, 277);
		container.add(scrollPane);

		

		JButton btnBack = new JButton("Back");
		btnBack.setBounds(638, 403, 183, 31);
		getContentPane().add(btnBack);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(171, 321, 91, 14);
		getContentPane().add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(171, 352, 82, 14);
		getContentPane().add(lblLastName);
		
		JLabel lblCnp = new JLabel("CNP");
		lblCnp.setBounds(408, 321, 46, 14);
		getContentPane().add(lblCnp);
		
		textFieldCnp = new JTextField();
		textFieldCnp.setBounds(452, 318, 142, 20);
		getContentPane().add(textFieldCnp);
		textFieldCnp.setColumns(10);
		
		textFieldLastName = new JTextField();
		textFieldLastName.setBounds(244, 349, 142, 20);
		getContentPane().add(textFieldLastName);
		textFieldLastName.setColumns(10);
		
		textFieldFirstName = new JTextField();
		textFieldFirstName.setBounds(244, 318, 142, 20);
		getContentPane().add(textFieldFirstName);
		textFieldFirstName.setColumns(10);

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				try {
					DataBaseConnection connection = new DataBaseConnection();
					connection.connected();
					int row = table.getSelectedRow();
					String tableClick = (table.getModel().getValueAt(row, 0).toString());
					String sql = "SELECT * FROM studentsregistration WHERE id = '" + tableClick + "' ";
					DataBaseConnection.statementInsert = DataBaseConnection.connection.prepareStatement(sql);
					ResultSet rs = DataBaseConnection.statementInsert.executeQuery(sql);
					
					if(rs.next()){
					    idCol = rs.getString("id");
						String firstNameCol = rs.getString("firstName");
						textFieldFirstName.setText(firstNameCol);
						String lastNameCol = rs.getString("lastName");
						textFieldLastName.setText(lastNameCol);
						String cnpCol = rs.getString("cnp");
						textFieldCnp.setText(cnpCol);
						String statusCol = rs.getString("status");
						textFieldStatus.setText(statusCol);
						String groupCol = rs.getString("group");
						textFieldGroup.setText(groupCol);
						String facultyNameCol = rs.getString("facultyName");
						textFieldFaculty.setText(facultyNameCol);
					}
				}catch(Exception e) {
					JOptionPane.showMessageDialog(null, e);
				}
			}
		});
					
		JButton updateButton = new JButton("Update registration");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent E) {
				
				String id = idCol;
				String firstNameCol = textFieldFirstName.getText();
				String lastNameCol = textFieldLastName.getText();
				String cnpCol = textFieldCnp.getText();
				String statusCol = textFieldStatus.getText();
				String groupCol = textFieldGroup.getText();
				String facultyCol = textFieldFaculty.getText();
				
				
				DataBaseConnection dataBase = new DataBaseConnection();
				dataBase.connected();
				try {
					
					dataBase.updateQueryStudents(id, firstNameCol, lastNameCol, cnpCol, statusCol, groupCol, facultyCol);
					
					JOptionPane.showMessageDialog(frame,"Registration updated!");
					
					dataBase.queryUpdate();
					
					setVisible(false);
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				UpdateStudentsDataBase updateDataBase = new UpdateStudentsDataBase();
				updateDataBase.setVisible(true);
			}
		});
		
		
		
		updateButton.setBounds(244, 403, 183, 31);
		getContentPane().add(updateButton);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(408, 352, 46, 14);
		getContentPane().add(lblStatus);
		
		textFieldStatus = new JTextField();
		textFieldStatus.setBounds(452, 349, 142, 20);
		getContentPane().add(textFieldStatus);
		textFieldStatus.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Group");
		lblNewLabel.setBounds(623, 321, 46, 14);
		getContentPane().add(lblNewLabel);
		
		textFieldGroup = new JTextField();
		textFieldGroup.setBounds(679, 318, 142, 20);
		getContentPane().add(textFieldGroup);
		textFieldGroup.setColumns(10);
		
		JLabel lblFaculty = new JLabel("Faculty");
		lblFaculty.setBounds(622, 352, 46, 14);
		getContentPane().add(lblFaculty);
		
		textFieldFaculty = new JTextField();
		textFieldFaculty.setBounds(678, 349, 143, 20);
		getContentPane().add(textFieldFaculty);
		textFieldFaculty.setColumns(10);
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				Ums frame = new Ums();
				frame.setVisible(true);
			}
		});

		

			

		
	}

	public UpdateStudentsDataBase(String id, String firstName, String lastName,
			String cnp, String status) {

		super();
		

		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.cnp = cnp;
		this.status = status;

		model = new DefaultTableModel();
		model.addColumn("Id");
		model.addColumn("First Name");
		model.addColumn("Last Name");
		model.addColumn("CNP");
		model.addColumn("Status");
		model.addColumn("Faculty Name");
		model.addColumn("Specialization");
		model.addColumn("Registration Year");
		model.addColumn("Year of study");
		model.addColumn("Group");

		

		table = new JTable(model);
		
		
	
	}
}
